package com.pri.chat.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.DTBean;
import com.netease.nim.uikit.net.entity.MsgTiShiBean;
import com.netease.nim.uikit.net.entity.NoticeUnreadNumber;
import com.netease.nim.uikit.net.entity.SystemMsgBean;
import com.netease.nim.uikit.net.entity.WishListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.MainActivity;
import com.pri.chat.R;
import com.pri.chat.adapter.SystemMsgAdapter;
import com.pri.chat.application.APP;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.bean.KeyAndValueEntity;
import com.pri.chat.database.greendao.KeyAndValueEntityDao;
import com.pri.chat.fragment.MainFragment;
import com.pri.chat.fragment.VideoFragment1;
import com.pri.chat.fragment.WishFragment;
import com.pri.chat.utils.Des3Util;
import com.vondear.rxtool.RxActivityTool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/5/9
 */
public class PushMsgActivity extends BaseActivity {
    @BindView(R.id.zanLinear)
    View zanLinear;
    @BindView(R.id.pingLunLinear)
    View pingLunLinear;
    @BindView(R.id.sysLinear)
    View sysLinear;
    @BindView(R.id.guanzhuLinear)
    View guanzhuLinear;
    @BindView(R.id.wishLinear)
    View wishLinear;

    @BindView(R.id.zanNum)
    TextView zanNum;
    @BindView(R.id.plNum)
    TextView plNum;
    @BindView(R.id.gzNum)
    TextView gzNum;
    @BindView(R.id.wishNum)
    TextView wishNum;
    @BindView(R.id.msgNum)
    TextView msgNum;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_push_msg_layout;
    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("NUM_CHANGE")) {
                if(gzNum != null && zanNum != null && plNum != null && wishNum != null && msgNum != null){
                    initInfo();
                }
            }
        }
    }

    private MyReceiver myReceiver;
    @Override
    protected void initView() {
        setTitle("系统消息");
        initLayout();
        IntentFilter filter = new IntentFilter();
        filter.addAction("NUM_CHANGE");
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, filter);
    }



    private List<SystemMsgBean> mList;
    private SystemMsgAdapter mAdapter;
    private int page = 1;
    private boolean isShowDialog = false;

    private void initLayout() {

    }

    @Override
    protected void initData() {

    }

    public void insertData(MsgTiShiBean tishi){
        zanNum.setText(tishi.getZanNum()+"");
        plNum.setText(tishi.getPlNum()+"");
        gzNum.setText(tishi.getGzNum()+"");
        wishNum.setText(tishi.getWishNum()+"");
        msgNum.setText(tishi.getMsgNum()+"");

        SharedHelper.saveKeyValueForString(activity,"unreadnum",tishi.getTotal()+"");

        zanNum.setVisibility(tishi.getZanNum()==0?View.INVISIBLE:View.VISIBLE);
        plNum.setVisibility(tishi.getPlNum()==0?View.INVISIBLE:View.VISIBLE);
        gzNum.setVisibility(tishi.getGzNum()==0?View.INVISIBLE:View.VISIBLE);
        wishNum.setVisibility(tishi.getWishNum()==0?View.INVISIBLE:View.VISIBLE);
        msgNum.setVisibility(tishi.getMsgNum()==0?View.INVISIBLE:View.VISIBLE);
    }
    
    private void getData(){
        SubscriberOnNextListener<BaseBean<MsgTiShiBean>> onNextListener = baseBean -> {
            insertData(baseBean.getData());
        };
        Map<String,Object> dtModel = new HashMap<>();//登录上传参数
        dtModel.put("memberId",SharedHelper.readId(this));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().getMsgTipNum(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);
    }

    @OnClick({R.id.zanLinear,R.id.pingLunLinear,R.id.sysLinear, R.id.guanzhuLinear, R.id.wishLinear})
    public void onViewClick(View v) {
        if(v.getId() == R.id.zanLinear){
            Bundle bundle = new Bundle();
            bundle.putInt("type",0);
            RxActivityTool.skipActivity(mContext, ZanOrPlPushMsgActivity.class,bundle);
        }else if(v.getId() == R.id.pingLunLinear){
            Bundle bundle = new Bundle();
            bundle.putInt("type",1);
            RxActivityTool.skipActivity(mContext, ZanOrPlPushMsgActivity.class,bundle);
        }else if(v.getId() == R.id.guanzhuLinear){
            RxActivityTool.skipActivity(mContext, GuanZhuPushMsgActivity.class);
        }else if(v.getId() == R.id.wishLinear){
            RxActivityTool.skipActivity(mContext, WishPushMsgActivity.class);
        }else if(v.getId() == R.id.sysLinear){
            RxActivityTool.skipActivity(mContext, NoticePushMsgActivity.class);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initInfo();
    }

    private void initInfo(){
        KeyAndValueEntity kavObj = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("unreadnum")).build().list().get(0);
        NoticeUnreadNumber unreadNumber = new Gson().fromJson(kavObj.getValue(),NoticeUnreadNumber.class);

        gzNum.setVisibility(View.GONE);
        zanNum.setVisibility(View.GONE);
        plNum.setVisibility(View.GONE);
        wishNum.setVisibility(View.GONE);
        msgNum.setVisibility(View.GONE);


        if(unreadNumber.getGzNum() > 0){
            gzNum.setText(unreadNumber.getGzNum()+"");
            gzNum.setVisibility(View.VISIBLE);
        }
        if(unreadNumber.getZanNum() > 0){
            zanNum.setText(unreadNumber.getZanNum()+"");
            zanNum.setVisibility(View.VISIBLE);
        }
        if(unreadNumber.getPlNum() > 0){
            plNum.setText(unreadNumber.getPlNum()+"");
            plNum.setVisibility(View.VISIBLE);
        }
        if(unreadNumber.getWishNum() > 0){
            wishNum.setText(unreadNumber.getWishNum()+"");
            wishNum.setVisibility(View.VISIBLE);
        }
        if(unreadNumber.getXttzNum() > 0){
            msgNum.setText(unreadNumber.getXttzNum()+"");
            msgNum.setVisibility(View.VISIBLE);
        }
    }
}
